home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / runtime / fail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-03  |  1.2 KB  |  71 lines  |  [TEXT/R*ch]

  1. /* Raising exceptions from C. */
  2.  
  3. #ifdef __MWERKS__
  4. #define MAXDOUBLE 1.7976931348623157081e+308
  5. #else
  6. #include <values.h>
  7. #endif
  8. #include "alloc.h"
  9. #include "fail.h"
  10. #include "memory.h"
  11. #include "mlvalues.h"
  12. #include "signals.h"
  13.  
  14.  
  15. /* The exception (Fail "floating point error") will be raised if
  16.    smlexn has not been initialized before a floating point error
  17.    occurs.  */
  18.  
  19. volatile unsigned char float_exn = FAILURE_EXN;
  20.  
  21. double maxdouble = MAXDOUBLE/2;
  22.  
  23. struct longjmp_buffer * external_raise;
  24. value exn_bucket;
  25.  
  26. void mlraise(v)
  27.      value v;
  28. {
  29.   in_blocking_section = 0;
  30.   exn_bucket = v;
  31.   longjmp(external_raise->buf, 1);
  32. }
  33.  
  34. void raise_with_arg(tag, arg)
  35.      tag_t tag;
  36.      value arg;
  37. {
  38.   value bucket;
  39.   Push_roots (a, 1);
  40.   a[0] = arg;
  41.  
  42.   bucket = alloc (1, tag);
  43.   Field(bucket, 0) = a[0];
  44.   Pop_roots ();
  45.   mlraise(bucket);
  46. }
  47.  
  48. void raise_with_string(tag, msg)
  49.      tag_t tag;
  50.      char * msg;
  51. {
  52.   raise_with_arg(tag, copy_string(msg));
  53. }
  54.  
  55. void failwith (msg)
  56.      char * msg;
  57. {
  58.   raise_with_string(FAILURE_EXN, msg);
  59. }
  60.  
  61. void invalid_argument (msg)
  62.      char * msg;
  63. {
  64.   raise_with_string(INVALID_EXN, msg);
  65. }
  66.  
  67. void raise_out_of_memory()
  68. {
  69.   mlraise(Atom(OUT_OF_MEMORY_EXN));
  70. }
  71.